package com.idealighter.game.games.horserace;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.HorseRaceRoomDomain;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.gamehall.dto.TableInfo;
import com.idealighter.game.games.horserace.message.ReqBetMsg;
import com.idealighter.game.games.horserace.message.ReqEnterGameHallMsg;
import com.idealighter.game.games.horserace.message.ReqEnterRoomMsg;
import com.idealighter.game.games.horserace.message.ReqEnterTableMsg;
import com.idealighter.game.games.horserace.message.ReqExchangeChipsMsg;
import com.idealighter.game.robot.core.Player;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.List;

/**
 * 赛马机器人.
 * 
 * @author zsc .
 * @date 2016年3月24日 上午10:56:07
 *
 */
public class HorseRacePlayer extends Player {

  public static final int BET = 2;
  public int state;

  public HorseRaceRoomDomain roomDomain;

  /**
   * 构造函数.
   * 
   * @param playerId .
   * @param userName .
   * @param roomId .
   * @param initGold .
   * @param totalGameNum .
   * @param tableGameNum .
   */
  public HorseRacePlayer(long playerId, String userName, int roomId, int initGold, int totalGameNum,
      int tableGameNum, RobotConfigDomain configDomain, HorseRaceRoomDomain roomDomain) {
    super(playerId, userName, roomId, initGold, totalGameNum, tableGameNum, configDomain);
    this.roomDomain = roomDomain;
  }

  @Override
  public boolean goldCheck() {
    long gold = playerInfo.getGold();
    if (gold < roomDomain.getLower()) {
      LOG.info("[赛马]机器人[{}][{}]金币小于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold > roomDomain.getUpper()) {
      LOG.info("[赛马]机器人[{}][{}]金币高于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold < roomDomain.getAfee()) {
      LOG.info("[赛马]机器人[{}][{}]金币小于台费退出游戏", userName, playerId);
      delayLogout();
      return false;
    }
    return true;
  }

  @Override
  public void enterGameHall() {
    sendMsg(new ReqEnterGameHallMsg());
  }

  @Override
  public void enterRoom() {
    if (rooms.contains(roomId)) {
      if (!goldCheck()) {
        return;
      }
      ReqEnterRoomMsg msg = new ReqEnterRoomMsg();
      msg.setRoomId(roomId);
      sendMsg(msg);
    }

  }

  @Override
  public void enterTable() {
    if (!goldCheck()) {
      return;
    }
    SeatInfo emptySeat = findEmptySeat();
    if (emptySeat != null) {
      ReqEnterTableMsg msg = new ReqEnterTableMsg();
      msg.setTableId((int) emptySeat.getTableId());
      sendMsg(msg);
      return;
    }
  }

  /**
   * 下注 .
   */
  public void dobet() {
    MemInfo memInfo = member(playerId);
    if (memInfo.getChips() <= 0) {
      return;
    }
    /*
     * if (state != BET && stepFuture != null) { stepFuture.cancel(false); return; }
     */
    if (state != BET) {
      return;
    }
    ReqBetMsg msg = new ReqBetMsg();
    int areA = RandCodeUtil.random(1, 15);
    long bet = bet();
    msg.setArea(areA);
    msg.setChips(bet);
    sendMsg(msg);
  }

  /**
   * 下注筹码.
   *
   * @return .
   */
  private long bet() {
    long chips = RandCodeUtil.random(1, 10000);
    MemInfo memInfo = member(playerId);
    if (memInfo.getChips() < chips) {
      return 0;
    }
    return chips;
  }

  /**
   * 兑换筹码.
   * 
   * @param player .
   */
  public void exchangeChips(Player player) {
    ReqExchangeChipsMsg exchangeChipsMsg = new ReqExchangeChipsMsg();
    exchangeChipsMsg.setGold(playerInfo.getGold());
    sendMsg(exchangeChipsMsg);
    LOG.info("[赛马]玩家[{}][{}]兑换筹码", userName, playerId);
  }

  /**
   * 获取当前桌子.
   * 
   * @return .
   */
  public TableInfo table() {
    long playerId = playerInfo.getId();

    for (TableInfo table : tables.values()) {
      List<SeatInfo> seats = table.getSeats();
      for (SeatInfo s : seats) {
        if (s.getPlayerId() == playerId) {
          return table;
        }
      }
    }

    return null;
  }

  @Override
  public Game game() {
    return Game.HORCERACE;
  }

  @Override
  public boolean enterPwdTableAble() {
    return true;
  }

}
